<?php
	class M_user extends CI_Model 
	{
		private $table_name = 'users';
		function __construct()
		{
			parent::__construct();
		}
		
		function updateLastVisited($userid = false, $time = false)
		{
			$userid = ($userid === false) ? $this->session->userdata('id_user') : $userid;
			$time = ($time === false) ? date('Y-m-d H:i:s') : (is_numeric($time) ? date('Y-m-d H:i:s', $time) : $time);
			if($userid && $time)
			{
				$this->db->update($this->table_name, array('last_visited' => $time), array('userid' => $userid));
				return ($this->db->affected_rows() == 1);
			}
			else return FALSE;
		}
	
		function checkExistingEmail($email)
		{
			$res = $this->db->get_where($this->table_name, array('email' => $email));
			return ($res->num_rows() == 1);
		}
		
		function checkActiveEmail($email)
		{
			$res = $this->db->get_where($this->table_name, array('email' => $email, 'is_active' => 'active'));
			return ($res->num_rows() == 1);
		}
		
		function getUserIdForCredential($email, $pass)
		{
			$this->load->library('encrypt');
			$pass = md5($pass);
			
			$res = $this->db->get_where($this->table_name, array('email' => $email, 'password' => $pass));
			if ($res->num_rows() == 1)
			{
				$info = $res->row();
				$res->free_result();
				return $info->id_user;
			}
			else return FALSE;
		}
		
		function insertUser($data = array())
		{
			if(is_array($data) and count($data) > 0)
			{
				$this->load->library('encrypt');
				$data['password'] = md5($data['password']);
				$this->db->insert($this->table_name, $data);
				return $this->db->insert_id();
			}
			else return FALSE;
		}
		
		function checkRandomNumberExists($num)
		{
			if($num)
			{
				$res = $this->db->get_where($this->table_name, array('random' => $num));
				return ($res->num_rows() > 0);
			}
			return FALSE;
		}
		
		function activateRandomNumber($num)
		{
			$res = $this->db->get_where($this->table_name, array('random' => $num));
			if($res->num_rows() == 1)
			{
				$info = $res->row();
				$res->free_result();
				$this->db->update($this->table_name, array('random' => '', 'is_active' => 'active'), array('random' => $num));
				if($this->db->affected_rows()) return $info->id_user;
			}
			return FALSE;
		}
		
		function loginWithUserid($userid)
		{
			$res = $this->db->get_where($this->table_name, array('id_user' => $userid));
			if($res->num_rows() == 1)
			{
				$info = $res->row();
				$res->free_result();
				
				$this->load->library('encrypt');
				$sess['userinfo'] = array();
				foreach($info as $k => $v) $sess['userinfo'][$k] = $v;
				$sess['id_user'] = $info->id_user;
				//$sess['userinfo']['password'] = $this->encrypt->decode($sess['userinfo']['password']);
				$this->session->set_userdata($sess);
				return TRUE;
			}
			return FALSE;
		}
		
		function logoutUser()
		{
			$this->session->unset_userdata('id_user');
			$this->session->unset_userdata('userinfo');
		}
	}